
package com.axc.gallery2.view;


import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.List;

import com.axc.gallery2.dbhelper.ImageInfo;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;



public class ImageAdapter extends BaseAdapter {
	private Context mContext;
	protected int editPositon =-1;
	public Context getContext() {
		return this.mContext; 
	}

	public void setContext(Context context) {
        this.mContext =context;
    }
	List<ImageInfo> list;
	private int screenWidth;
	private int screenHeight;

	public List<ImageInfo> getList(){
		return list;
	}

	public ImageAdapter(Context context,List<ImageInfo> lists) {
		mContext = context;		
		list=lists;
	}




	public int getCount() {
		return list.size();
	}

	public Object getItem(int position) {
		return list.get(position);
	}

	public void setSize(int w, int h) {
		screenWidth =w;
		screenHeight = h;
	}
	public long getItemId(int position) {
		return position;
	}

	public void removeAt(int position) {
		convertviewList.remove(position);
		list.remove(position);
		notifyDataSetChanged();
	}
	public void editItem(int position,Object item) {
		ImageInfo imageInfo=new ImageInfo();
		imageInfo.image_path=(String) item;
		list.set(position, imageInfo);
		notifyDataSetChanged();
	}
	public void add(Object item) {
		ImageInfo imageInfo= (ImageInfo) item;

		list.add(getCount(),imageInfo);
		notifyDataSetChanged();
	}
	List<SoftReference<View>> convertviewList =new ArrayList<SoftReference<View>>();

	public View getView(int position, View convertView, ViewGroup parent) {
		try {
			if(convertviewList.size()-1>= position){
				convertView = convertviewList.get(position).get();
				if(convertView==null){
					convertView =iniCovertView(convertView, position, 0);
				}
			}else {
				convertView =iniCovertView(convertView, position, 1);
			}		
			return convertView;
		} 
		catch(Exception e){
			System.out.println(e.getMessage());
		}
		return convertView;
	}
	/**
	 * 添加项目对象
	 * 
	 * @param convertView
	 * @param position
	 * @param oper
	 *            1为添加对象，其他为修改对象
	 * @return
	 */
	protected View iniCovertView(View convertView,int position,int oper) {
		ViewHolder viewHolder =new ViewHolder();
		convertView = new ImageView(mContext);
		convertView.setTag(viewHolder);
		viewHolder.imageView = (ImageView) convertView;
		new AsyncLoadImage().execute(new Object[] { viewHolder.imageView,
                list.get(position) });
		convertView.setLayoutParams(new Gallery.LayoutParams(screenWidth*2/5,screenHeight*5/12));
		
		if(oper==1){
			convertviewList.add(new SoftReference<View>(convertView));
		}
		else {
			convertviewList.set(position,new SoftReference<View>(convertView));
		}
		System.out.println(convertView==null);
		return convertView;
	}
	public static class ViewHolder { 
		public ImageView imageView; 
		public CheckBox checkBox;
		public TextView idTextView;
		public TextView nameTextView;
		public TextView drTextView;
		public TextView countTextView;
		public ProgressBar progressBar;
		public Button button;
		public Button button1;
		public ViewHolder() {
			
		}
	}



}
